// Copyright (c) 2015 Cesanta Software Limited
// All rights reserved

#include "mongoose.h"
#include "webpages.h"
#include <stdlib.h>
#include <stdio.h>
#include "cJSON.h"

static const char *s_http_port = "8000";
//static struct mg_serve_http_opts s_http_server_opts;

void ev_handler(struct mg_connection *nc, int ev, void *p) {


  switch (ev) {
    case MG_EV_ACCEPT: {
      char addr[32];
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
      printf("Connection %p from %s\n", nc, addr);
      break;
    }
    case MG_EV_HTTP_REQUEST: {
      char addr[32];
      struct http_message *hm = (struct http_message *) p;
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
      printf("HTTP request from %s: %.*s %.*s %.*s\n", addr, (int) hm->method.len,
             hm->method.p, (int) hm->uri.len, hm->uri.p, (int)hm->query_string.len, hm->query_string.p);
      //mg_printf(nc, reply_fmt, addr);
      //printf("")
      //printf("parse len:%d\n", mg_get_http_var(&hm->query_string, "comman", value, 32));
      //printf("value:%s\n",value);
      mg_send_head(nc, 404, 0, "");
      nc->flags |= MG_F_SEND_AND_CLOSE;
      break;
    }
    case MG_EV_CLOSE: {
      printf("Connection %p closed\n", nc);
      break;
    }
  }
}

static void handle_index(struct mg_connection *nc, int ev, void *ev_data) {
	(void) ev; (void) ev_data;
	mg_send_head(nc, 200, index_html_len, "");
	//mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello1]");
	mg_send(nc, index_html, index_html_len);
	nc->flags |= MG_F_SEND_AND_CLOSE;
	printf("send the index html!\r\n");
}

static void handle_config(struct mg_connection *nc, int ev, void *ev_data) {
	(void) ev; (void) ev_data;
	char value[32] = {0};
	struct http_message *hm = (struct http_message *)ev_data;
	cJSON *root = NULL; char *out;
	int rs = 0;
	
	rs = mg_get_http_var(&hm->query_string, "command", value, 32);
	if(rs < 0){
		mg_send_head(nc, 404, 0, "");
	}	
	else{
		root=cJSON_CreateObject();
		if(root == NULL){
			printf("err to create json object\n");
			mg_send_head(nc, 404, 0, "");
		}
		else{
			cJSON_AddNumberToObject(root, "baud", 1);
			cJSON_AddNumberToObject(root, "dbits", 1);
			cJSON_AddNumberToObject(root, "sbits", 1);
			cJSON_AddNumberToObject(root, "parity", 1);
			out = cJSON_Print(root);
			printf("json : %s", out);
			mg_send_head(nc, 200, strlen(out), "");
			mg_send(nc, out, strlen(out));
			cJSON_Delete(root);	printf("%s\n",out);	free(out);	/* Print to text, Delete the cJSON, print it, release the string. */

		}	

	}
	//mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello1]");
	//mg_send(nc, components_webpages_index_html, components_webpages_index_html_len);
	
	nc->flags |= MG_F_SEND_AND_CLOSE;
	printf("send the config response!\r\n");
}

int main(void) {
  struct mg_mgr mgr;
  struct mg_connection *nc;

  mg_mgr_init(&mgr, NULL);
  printf("Starting web server on port %s\n", s_http_port);
  nc = mg_bind(&mgr, s_http_port, ev_handler);
  if (nc == NULL) {
    printf("Failed to create listener\n");
    return 1;
  }
  mg_register_http_endpoint(nc, "/ ", handle_index);
  mg_register_http_endpoint(nc, "/index.html", handle_index);
  mg_register_http_endpoint(nc, "/config", handle_config);

  // Set up HTTP server parameters
  mg_set_protocol_http_websocket(nc);
  //s_http_server_opts.document_root = ".";  // Serve current directory
  //s_http_server_opts.enable_directory_listing = "no";

  for (;;) {
    mg_mgr_poll(&mgr, 1000);
  }
  mg_mgr_free(&mgr);

  return 0;
}
